package com.huilai.platform.country_platform.service;

import com.huilai.platform.country_platform.model.AttractionsBuilding;
import com.huilai.platform.country_platform.model.PartyActivities;
import com.huilai.platform.country_platform.model.User;
import com.huilai.platform.country_platform.model.VillageActivities;
import com.huilai.platform.country_platform.repository.PartyActivitiesReposity;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/3/6
 * 注释：
 */
@Service
public class PartyService {

    @Autowired
    private PartyActivitiesReposity partyActivitiesReposity;

    public List<PartyActivities> getPartyActivitiesList(String contentType){
        PageRequest pageable = PageRequest.of(0, 7);
        Page<PartyActivities> partyActivitiesPage = partyActivitiesReposity.findAll((root, query, cb) ->{
                    List<Predicate> predicates = new ArrayList<>();
                    predicates.add(cb.equal(root.get("contentType"),contentType));
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                ,pageable);
        return partyActivitiesPage.getContent();
    }

    public Page<PartyActivities> activitiesUI(String searchName, String contentType, Pageable pageable) {
        Page<PartyActivities> partyActivitiesPage = partyActivitiesReposity.findAll((root, query, cb) -> {
                    List<Predicate> predicates = new ArrayList<>();
                    //模糊查询
                    if (!StringUtils.isEmpty(searchName)) {
                        predicates.add(cb.or(
                                (cb.like(root.get("title"), "%" + searchName + "%")),
                                (cb.like(root.get("describe"), "%" + searchName + "%"))));
                    }
                    predicates.add(cb.equal(root.get("contentType"),contentType));
                    query.where(predicates.toArray(new Predicate[]{}));
                    query.orderBy(cb.desc(root.get("updateTime")));
                    return null;
                }
                , pageable);
        return partyActivitiesPage;
    }

    public Integer addPartyActivities(PartyActivities partyActivities){
//        userRepository.save(user);
        if (partyActivities.getImage().equals("example")){
            partyActivities.setImage(null);
        }
        if (partyActivities.getFile().equals("example")){
            partyActivities.setFile(null);
        }
        Integer integer = partyActivitiesReposity.addPartyActivities(partyActivities);
        return integer;
    }

    public PartyActivities getActivitiesById (Integer id){
        PartyActivities partyActivities = partyActivitiesReposity.getOne(id);
        return partyActivities;
    }

    public Integer updateActivities(PartyActivities partyActivities){
        if (partyActivities.getImage().equals("example")){
            partyActivities.setImage(null);
        }
        if (partyActivities.getFile().equals("example")){
            partyActivities.setFile(null);
        }
        Integer integer = partyActivitiesReposity.updateActivities(partyActivities);
        return integer;
    }

    public Integer deleteActivitiesById(Integer id){
        Integer integer = partyActivitiesReposity.deleteActivitiesById(id);
        return integer;
    }

    public PartyActivities findActivitiesyId(Integer id){
        return partyActivitiesReposity.findById(id).orElseThrow(() -> new ServiceException("党员活动id错误!"));

    }
}
